package com.cheyunkeji.er.activity.evaluate;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.view.TopBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CarFrameworkBZSM extends BaseActivity implements TextWatcher {


    //    添加不对称状态标识
    public static final int CTZYDC_BDC = 7;
    private static final String TAG = CarFrameworkBZSM.class.getSimpleName();
    private static final int REQUEST_CODE_EDIT_REMARKS = 0X1101 ;
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tv_ctzydc)
    TextView tvCtzydc;
    @BindView(R.id.iv_ctzydc_ok)
    ImageView ivCtzydcOk;
    @BindView(R.id.et_ctzydc)
    TextView etCtzydc;
    @BindView(R.id.tv_yaz)
    TextView tvYaz;
    @BindView(R.id.iv_yaz_ok)
    ImageView ivYazOk;
    @BindView(R.id.et_yaz)
    TextView etYaz;
    @BindView(R.id.tv_ybz)
    TextView tvYbz;
    @BindView(R.id.iv_ybz_ok)
    ImageView ivYbzOk;
    @BindView(R.id.et_ybz)
    TextView etYbz;
    @BindView(R.id.tv_ycz)
    TextView tvYcz;
    @BindView(R.id.iv_ycz_ok)
    ImageView ivYczOk;
    @BindView(R.id.et_ycz)
    TextView etYcz;
    @BindView(R.id.tv_yqxg)
    TextView tvYqxg;
    @BindView(R.id.iv_yqxg_ok)
    ImageView ivYqxgOk;
    @BindView(R.id.yqxg)
    TextView yqxg;
    @BindView(R.id.tv_yhxg)
    TextView tvYhxg;
    @BindView(R.id.iv_yhxg_ok)
    ImageView ivYhxgOk;
    @BindView(R.id.et_yhxg)
    TextView etYhxg;
    @BindView(R.id.tv_yqzl)
    TextView tvYqzl;
    @BindView(R.id.iv_yqzl_ok)
    ImageView ivYqzlOk;
    @BindView(R.id.et_yqzl)
    TextView etYqzl;
    @BindView(R.id.tv_yyl)
    TextView tvYyl;
    @BindView(R.id.iv_yyl_ok)
    ImageView ivYylOk;
    @BindView(R.id.et_yyl)
    TextView etYyl;
    @BindView(R.id.tv_zaz)
    TextView tvZaz;
    @BindView(R.id.iv_zaz_ok)
    ImageView ivZazOk;
    @BindView(R.id.et_zaz)
    TextView etZaz;
    @BindView(R.id.tv_zbz)
    TextView tvZbz;
    @BindView(R.id.iv_zbz_ok)
    ImageView ivZbzOk;
    @BindView(R.id.et_zbz)
    TextView etZbz;
    @BindView(R.id.tv_zcz)
    TextView tvZcz;
    @BindView(R.id.iv_zcz_ok)
    ImageView ivZczOk;
    @BindView(R.id.et_zcz)
    TextView etZcz;
    @BindView(R.id.tv_zqxg)
    TextView tvZqxg;
    @BindView(R.id.iv_zqxg_ok)
    ImageView ivZqxgOk;
    @BindView(R.id.et_zqxg)
    TextView etZqxg;
    @BindView(R.id.tv_zhxg)
    TextView tvZhxg;
    @BindView(R.id.iv_zhxg_ok)
    ImageView ivZhxgOk;
    @BindView(R.id.et_zhxg)
    TextView etZhxg;
    @BindView(R.id.tv_zqzl)
    TextView tvZqzl;
    @BindView(R.id.iv_zqzl_ok)
    ImageView ivZqzlOk;
    @BindView(R.id.et_zqzl)
    TextView etZqzl;
    @BindView(R.id.tv_zyl)
    TextView tvZyl;
    @BindView(R.id.iv_zyl_ok)
    ImageView ivZylOk;
    @BindView(R.id.et_zyl)
    TextView etZyl;
    @BindView(R.id.tv_lmj)
    TextView tvLmj;
    @BindView(R.id.iv_lmj_ok)
    ImageView ivLmjOk;
    @BindView(R.id.lmj)
    TextView lmj;
    @BindView(R.id.activity_car_framework_bzsm)
    LinearLayout activityCarFrameworkBzsm;
    @BindView(R.id.sv_content)
    ScrollView svContent;
    private String[] proDesc;

    private ArrayList<EvaluateDetailResult2.DetectionBean.BodyframeBean> list;

    private ImageView[] imgList = null;

    private TextView[] tvList = null;

    private TextView[] etList = null;

    @Override
    protected void init() {

        setContentView(R.layout.er_activity_car_framework_bzsm);
        ButterKnife.bind(this);

        proDesc = getResources().getStringArray(R.array.problem_desc);

        imgList = new ImageView[]{ivCtzydcOk, ivYazOk, ivYbzOk, ivYczOk, ivYqxgOk, ivYhxgOk, ivYqzlOk, ivYylOk, ivZazOk, ivZbzOk, ivZczOk, ivZqxgOk, ivZhxgOk, ivZqzlOk, ivZylOk, ivLmjOk};
        tvList = new TextView[]{tvCtzydc, tvYaz, tvYbz, tvYcz, tvYqxg, tvYhxg, tvYqzl, tvYyl, tvZaz, tvZbz, tvZcz, tvZqxg, tvZhxg, tvZqzl, tvZyl, tvLmj};
        etList = new TextView[]{etCtzydc, etYaz, etYbz, etYcz, yqxg, etYhxg, etYqzl, etYyl, etZaz, etZbz, etZcz, etZqxg, etZhxg, etZqzl, etZyl, lmj};
    }

    @Override
    protected void initView() {

        vTopbar.setLeft("", R.mipmap.er_ic_back, this);

        vTopbar.setTitle("车身骨架备注说明");

        for (TextView tv : etList) {
            tv.setOnClickListener(this);
        }

        if (null != getIntent().getSerializableExtra("FRAMEWORK_CHECK_ITEMS")) {

            list = (ArrayList<EvaluateDetailResult2.DetectionBean.BodyframeBean>) getIntent().getSerializableExtra("FRAMEWORK_CHECK_ITEMS");

            for (int i = 0; i < list.size(); i++) {

                if (list.get(i).getId() == 0 && list.get(i).getStatus() == CTZYDC_BDC) {

                    imgList[0].setVisibility(View.GONE);
                    tvList[0].setVisibility(View.VISIBLE);
                    tvList[0].setText("不对称");
                    etList[0].setText(list.get(i).getMark());
                    continue;
                }

                if (list.get(i).getStatus() != 0) {
                    imgList[list.get(i).getId()].setVisibility(View.GONE);
                    tvList[list.get(i).getId()].setVisibility(View.VISIBLE);
                    tvList[list.get(i).getId()].setText(proDesc[list.get(i).getStatus() - 1]);
                    etList[list.get(i).getId()].setText(list.get(i).getMark());
                }
            }

        }

        etCtzydc.addTextChangedListener(this);

    }


    @Override
    public void onBackPressed() {

        takeDataBack();

    }

    private void takeDataBack() {
        Intent intent = new Intent();
        intent.putExtra("FRAMEWORK_CHECK_ITEMS", (Serializable) getResultData());
        setResult(RESULT_OK, intent);

        Log.e(TAG, "onBackPressed: 返回 ");
        MyApplication.getInstance().removeActivity(this.getClass());
        finish();
    }


    private List<EvaluateDetailResult2.DetectionBean.BodyframeBean> getResultData() {
        boolean isItem0Exist = false;
        for (int i = 0; i < list.size(); i++) {
            if (0 != list.get(i).getStatus() && !TextUtils.isEmpty(etList[list.get(i).getId()].getText().toString())) {
                list.get(i).setMark(etList[list.get(i).getId()].getText().toString());
            }
            if (list.get(i).getId() == 0) {
                isItem0Exist = true;
            }
        }
        if (!isItem0Exist && !TextUtils.isEmpty(etList[0].getText().toString())) {
            list.add(0, new EvaluateDetailResult2.DetectionBean.BodyframeBean(0, CTZYDC_BDC, etList[0].getText().toString()));
        }
        return list;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s.toString())) {
            imgList[0].setVisibility(View.GONE);
            tvList[0].setVisibility(View.VISIBLE);
            tvList[0].setText("不对称");
        } else {
            imgList[0].setVisibility(View.VISIBLE);
            tvList[0].setVisibility(View.INVISIBLE);
            tvList[0].setText("");
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.vLeft:
                takeDataBack();
                break;
            case R.id.et_ctzydc:
                goToEditText(etCtzydc.getText().toString(),etCtzydc);
                break;
            case R.id.et_yaz:
                goToEditText(etYaz.getText().toString(),etYaz);
                break;
            case R.id.et_ybz:
                goToEditText(etYbz.getText().toString(),etYbz);
                break;
            case R.id.et_ycz:
                goToEditText(etYcz.getText().toString(),etYcz);
                break;
            case R.id.et_yhxg:
                goToEditText(etYhxg.getText().toString(),etYhxg);
                break;
            case R.id.et_yqzl:
                goToEditText(etYqzl.getText().toString(),etYqzl);
                break;
            case R.id.et_yyl:
                goToEditText(etYyl.getText().toString(),etYyl);
                break;
            case R.id.et_zaz:
                goToEditText(etZaz.getText().toString(),etZaz);
                break;
            case R.id.et_zbz:
                goToEditText(etZbz.getText().toString(),etZbz);
                break;
            case R.id.et_zcz:
                goToEditText(etZcz.getText().toString(),etZcz);
                break;
            case R.id.et_zqxg:
                goToEditText(etZqxg.getText().toString(),etZqxg);
                break;
            case R.id.et_zhxg:
                goToEditText(etZhxg.getText().toString(),etZhxg);
                break;
            case R.id.et_zqzl:
                goToEditText(etZqzl.getText().toString(),etZqzl);
                break;
            case R.id.et_zyl:
                goToEditText(etZyl.getText().toString(),etZyl);
                break;
            case R.id.lmj:
                goToEditText(lmj.getText().toString(),lmj);
                break;
            case R.id.yqxg:
                goToEditText(yqxg.getText().toString(),yqxg);
                break;
            default:
                break;
        }

    }

    private void goToEditText(String remarksText,TextView tvRemarks) {
        svContent.setTag(tvRemarks);
        Intent intent = new Intent(this,EditRemarksActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(Constants.EDITING_CONTENT,remarksText);
        bundle.putString(Constants.EDITING_TITLE,"编辑备注");
        intent.putExtras(bundle);
        startActivityForResult(intent,REQUEST_CODE_EDIT_REMARKS);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE_EDIT_REMARKS && resultCode == RESULT_OK){
            String editedRemarks = data.getStringExtra(Constants.EDITED_CONTENT) ;
//            if(tvCtzydc == svContent.getTag()){
//
//            }
            ((TextView)svContent.getTag()).setText(editedRemarks);
        }
    }
}
